<?php

namespace app\admin\controller\xmwms;

use app\admin\model\xmwms\InventoryHistory;
use app\common\controller\Backend;
use app\admin\model\xmwms\WarehousingOrder;
use app\admin\model\xmwms\OutboundOrder;

class Dashboard extends Backend
{

    protected object $inventoryHistoryModel;

    protected object $outboundOrderModel;

    protected object $warehousingOrderModel;

    protected object $productModel;

    protected object $warehouseModel;

    protected object $customerModel;

    protected object $supplierModel;

    protected object $carrierModel;

    protected object $brandModel;

    public function initialize(): void
    {
        parent::initialize();
        $this->inventoryHistoryModel = new InventoryHistory();
        $this->warehousingOrderModel = new WarehousingOrder();
        $this->outboundOrderModel    = new OutboundOrder();
        $this->productModel          = new \app\admin\model\xmwms\Product();
        $this->warehouseModel        = new \app\admin\model\xmwms\Warehouse();
        $this->customerModel         = new \app\admin\model\xmwms\Customer();
        $this->supplierModel         = new \app\admin\model\xmwms\Supplier();
        $this->carrierModel          = new \app\admin\model\xmwms\Carrier();
        $this->brandModel            = new \app\admin\model\xmwms\Brand();
    }

    public function index(): void
    {
        $today        = date('Y-m-d');
        $inWarehouse  = [10, 20, 30];
        $outWarehouse = [40, 50, 60];

        $today_storage_volume       = $this->inventoryHistoryModel->whereDay('create_time', $today)->whereIn('form_type', $inWarehouse)->sum('quantity');
        $today_quantity_delivered   = $this->inventoryHistoryModel->whereDay('create_time', $today)->whereIn('form_type', $outWarehouse)->sum('quantity');
        $warehouse_entry_reviewed   = $this->warehousingOrderModel->where('audit_status', 0)->count();
        $warehouse_release_reviewed = $this->outboundOrderModel->where('audit_status', 0)->count();

        $months               = $this->getMonths();
        $inWarehouseQuantity  = [];
        $outWarehouseQuantity = [];
        $inWarehousePrice     = [];
        $outWarehousePrice    = [];
        foreach ($months as $k => $v) {
            $inWarehouseQuantity[]  = $this->inventoryHistoryModel
                ->whereIn('form_type', $inWarehouse)
                ->whereMonth('create_time', $v)
                ->sum('quantity');
            $outWarehouseQuantity[] = $this->inventoryHistoryModel
                ->whereIn('form_type', $outWarehouse)
                ->whereMonth('create_time', $v)
                ->sum('quantity');

            $in_warehouse_price  = $this->inventoryHistoryModel
                ->whereIn('form_type', $inWarehouse)
                ->whereMonth('create_time', $v)
                ->sum('total_price');
            $out_warehouse_price = $this->inventoryHistoryModel
                ->whereIn('form_type', $outWarehouse)
                ->whereMonth('create_time', $v)
                ->sum('total_price');
            $inWarehousePrice[]  = round($in_warehouse_price / 10000);
            $outWarehousePrice[] = round($out_warehouse_price / 10000);
        }

        $productInventory     = $this->productModel->where('total_stock_num', '>', 0)->select();
        $productInventoryData = [];
        foreach ($productInventory as $key => $val) {
            $productInventoryData[$key]['name']  = $val['name'];
            $productInventoryData[$key]['value'] = $val['total_stock_num'];
        }


        $warehouseStatistics = [
            ['name' => '仓库', 'total' => $this->warehouseModel->where('warehouse_type', 'warehouse')->count()],
            ['name' => '库区', 'total' => $this->warehouseModel->where('warehouse_type', 'warehouse_area')->count()],
            ['name' => '货架', 'total' => $this->warehouseModel->where('warehouse_type', 'warehouse_shelf')->count()],
            ['name' => '品牌', 'total' => $this->brandModel->count()],
            ['name' => '产品物料', 'total' => $this->productModel->count()],
            ['name' => '库存预警', 'total' => $this->productModel->whereColumn('safety_stock_num', '>', 'total_stock_num')->where('total_stock_num', '>', 0)->where('status', 10)->count()],
            ['name' => '供应商', 'total' => $this->supplierModel->count()],
            ['name' => '客户', 'total' => $this->customerModel->count()],
            ['name' => '承运商', 'total' => $this->carrierModel->count()],
        ];


        $this->success('', [
            'remark'               => get_route_remark(),
            'months'               => $this->getMonths('', 'y/m'),
            'dataStatistics'       => [
                'today_storage_volume'       => $today_storage_volume,
                'today_quantity_delivered'   => $today_quantity_delivered,
                'warehouse_entry_reviewed'   => $warehouse_entry_reviewed,
                'warehouse_release_reviewed' => $warehouse_release_reviewed
            ],
            'inventoryQuantity'    => [
                'inWarehouseQuantity'  => $inWarehouseQuantity,
                'outWarehouseQuantity' => $outWarehouseQuantity
            ],
            'inventoryPrice'       => [
                'inWarehousePrice'  => $inWarehousePrice,
                'outWarehousePrice' => $outWarehousePrice
            ],
            'productInventoryData' => $productInventoryData,
            'warehouseStatistics'  => $warehouseStatistics
        ]);
    }

    public function getMonths($time = '', $format = 'Y-m')
    {
        $time = $time != '' ? $time : time();
        //组合数据
        $month = [];
        for ($i = 1; $i <= 12; $i++) {
            $month[] = date($format, strtotime($i - 12 . ' month', $time));
        }
        return $month;
    }
}